import { usePermissionStore } from "@/store/modules/permission.store";
import { intersection, isArray } from "lodash-es";

export const usePermissions = () => {
  const permissionStore = usePermissionStore();

  const hasPermission = (value?: string | string[], def = true): boolean => {
    if (!value) return def;
    const allCodeList = permissionStore.getPermCodeList as string[];
    if (!isArray(value) && allCodeList && allCodeList.length > 0) {
      return allCodeList.includes(value);
    }

    return (intersection(value, allCodeList) as string[]).length > 0;
  };

  return {
    hasPermission,
  };
};
